﻿Imports HtmlAgilityPack
Imports System.Text
Imports System.Threading.Tasks
Imports System.Collections.Concurrent

Public Module GetTranslations

	Public Function GetLanguages() As List(Of KeyValuePair(Of String, Translation))
		Dim returnList As New List(Of KeyValuePair(Of String, Translation))

		Dim URL As String = "http://wol.jw.org/en/wol/li/r1/lp-e"
		Dim Page As String
		Dim doc As New HtmlDocument

		Using client As New System.Net.WebClient
			client.Encoding = System.Text.Encoding.UTF8
			Page = client.DownloadString(URL)
		End Using

		doc.LoadHtml(Page)

		For Each myNode As HtmlNode In doc.DocumentNode.SelectNodes("//div[@class='completeList']//ul[@class='librarySelection']//li[@role='row']")
			Dim path As String = "http://m.wol.jw.org"
			Dim LanguageCode As String
			Dim Language As String
			'Dim IsRTL As Boolean
			'Dim HasHebrew As Boolean
			'Dim HasGreek As Boolean

			path &= myNode.Descendants.Where(Function(x) x.Name = "a").Select(Function(x) x.Attributes("href").Value).FirstOrDefault.Split("?")(0)
			LanguageCode = myNode.Descendants.Where(Function(x) x.Attributes("data-lang") IsNot Nothing).Select(Function(x) x.Attributes("data-lang").Value).FirstOrDefault
			Language = myNode.Descendants.Where(Function(x) x.Attributes("class") IsNot Nothing AndAlso x.Attributes("class").Value = "language").Select(Function(x) x.InnerText).FirstOrDefault

			returnList.Add(New KeyValuePair(Of String, Translation)(path, New Translation(LanguageCode, Language, False, False, False)))

		Next

		Return returnList

	End Function

	Public Function GetLanguageData(myLanguages As List(Of KeyValuePair(Of String, Translation))) As List(Of KeyValuePair(Of String, Translation))
		Dim returnList As New ConcurrentBag(Of KeyValuePair(Of String, Translation))()

		Dim Page As String
		Dim doc As New HtmlDocument

		Parallel.ForEach(myLanguages, Sub(myLangauge)
										  Using client As New System.Net.WebClient
											  client.Encoding = System.Text.Encoding.UTF8
											  Page = client.DownloadString(myLangauge.Key)
										  End Using

										  doc.LoadHtml(Page)

										  If doc.DocumentNode.SelectSingleNode("//li[@id='menuBible'][@class='disabled']") Is Nothing Then
											  Dim URL As String
											  Dim IsRTL As Boolean
											  Dim HasHebrew As Boolean
											  Dim HasGreek As Boolean

											  Dim TextDirection As String
											  TextDirection = doc.DocumentNode.SelectSingleNode("/html[@dir]").Attributes("dir").Value

											  If TextDirection = "rtl" Then
												  IsRTL = True
											  Else
												  IsRTL = False
											  End If

											  URL = String.Format("http://m.wol.jw.org{0}", doc.DocumentNode.SelectSingleNode("//li[@id='menuBible']/a").Attributes("href").Value)

											  Using client As New System.Net.WebClient
												  client.Encoding = System.Text.Encoding.UTF8
												  Page = client.DownloadString(URL)
											  End Using

											  doc.LoadHtml(Page)

											  If doc.DocumentNode.SelectSingleNode("//ul[@class='books hebrew']") Is Nothing Then
												  HasHebrew = False
											  Else
												  HasHebrew = True
											  End If

											  HasGreek = True

											  URL = String.Format("http://m.wol.jw.org{0}", doc.DocumentNode.SelectSingleNode("//ul[@class='books greek']/li[1]/a | //ul[@class='books greekOnly']/li[1]/a").Attributes("href").Value)

											  Using client As New System.Net.WebClient
												  client.Encoding = System.Text.Encoding.UTF8
												  Page = client.DownloadString(URL)
											  End Using

											  doc.LoadHtml(Page)

											  URL = String.Format("http://m.wol.jw.org{0}", doc.DocumentNode.SelectSingleNode("//li[@class='chapter'][1]/a").Attributes("href").Value)
											  URL = URL.Substring(0, URL.Length - 4)

											  returnList.Add(New KeyValuePair(Of String, Translation)(URL, New Translation(myLangauge.Value.LanguageCode, myLangauge.Value.Language, IsRTL, HasHebrew, HasGreek)))

										  End If
									  End Sub)

		Return returnList.ToList

	End Function

End Module